package components.custom
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.collections.IList;
	import mx.core.FlexGlobals;
	import mx.core.IVisualElement;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	import mx.effects.easing.Linear;
	import mx.events.EffectEvent;
	import mx.events.ListEvent;
	
	import spark.components.BorderContainer;
	import spark.components.HGroup;
	import spark.effects.Move;
	import spark.layouts.HorizontalLayout;
	
	import framework.AppGlobal;
	
	[Event(name="itemClick", type="mx.events.ItemClickEvent")]
	public class MenuTopBar extends HGroup
	{
		/**
		 * 鼠标滑入时，延迟打开菜单的时间(单位：ms)
		 * */
		public var delay:Number = 200;
		/**
		 * 打开菜单延迟定时器
		 * */
		private var delayTimer:int;
		
		private const BAR_HEIGHT:Number = 26;
		
		public function MenuTopBar()
		{
			gap = 10;
		}
		
		/**
		 * 按钮的文本域
		 */		
		private var _labelField:String;
		public function set labelField(p_str:String):void
		{
			_labelField = p_str;
			removeAllElements();
			if(_dataProvider)
				createButtons();
		}
		public function get labelField():String
		{
			return _labelField;
		}
		
		/**
		 * data provider
		 * @param p_source
		 * 
		 */		
		private var _dataProvider:Object;
		[Bindable("change")]
		[Bindable("collectionChange")]
		public function set dataProvider(p_source:Object):void
		{
			_dataProvider = p_source;
			removeAllElements();
			if(_labelField)
				createButtons();
			dispatchEvent(new ListEvent("change"));
		}
		public function get dataProvider():Object
		{
			return _dataProvider;
		}
		
		private function get hLayout():HorizontalLayout
		{
			var hl:HorizontalLayout = new HorizontalLayout();
			hl.verticalAlign = "middle";
			hl.horizontalAlign = "center";
			hl.gap = 0;
			return hl;
		}
		
		/**
		 * 创建菜单按钮
		 * 
		 */		
		private function createButtons():void
		{
			var dp:IList = _dataProvider as IList;
			for(var i:uint = 0;i < dp.length;i ++){
				var cav:BorderContainer = new BorderContainer();
				cav.layout = hLayout;
				cav.percentHeight=100;
				cav.setStyle("backgroundAlpha", 0); 
				cav.setStyle("borderVisible", false);
				addElement(cav);
				
				//中间				
				cav.addElement(middleBtn(dp.getItemAt(i) as XML));				
			}
		}
		
		/**
		 * 分隔线
		 * @return 
		 * 
		 */		
		private function get strokeSeperator():BorderContainer
		{
			var cav:BorderContainer = new BorderContainer();
			cav.name = "seperator";
			cav.setStyle("backgroundAlpha", 1);
			cav.setStyle("borderVisible", false);
			cav.width = 1;
			cav.height = 26;
			return cav;
		}
		
		/**
		 * 中间按钮
		 * @return 
		 * 
		 */		
		private function middleBtn(p_data:XML):TopBarButton
		{
			var btn:TopBarButton = new TopBarButton();
			btn.percentWidth = 100;
			btn.percentHeight = 100;
			btn.setStyle("verticalAlign", "middle");
			btn.setStyle("textAlign", "center");
			btn.setStyle("fontSize", 14);
			btn.setStyle("color", 0xffffff);
			btn.text = p_data[_labelField];
			btn.data = p_data;
			btn.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			btn.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			return btn;
		}
		
		/**
		 * 左边圆角图标
		 * @return 
		 * 
		 */		
		private function get borderIcon():BorderContainer
		{
			var cav:BorderContainer = new BorderContainer();
			cav.setStyle("backgroundAlpha", 0);
			cav.setStyle("borderVisible", false);
			cav.width = 10;
			cav.height = BAR_HEIGHT;
			return cav;
		}
		
		/**
		 * 鼠标滑入事件
		 * @param p_evt
		 * 
		 */
		private function rollOverHandler(p_evt:MouseEvent):void
		{
			var btn:TopBarButton = p_evt.currentTarget as TopBarButton;
			if(btn){
				btn.isFocusIn = true;
				if(!btn.menuList || !btn.menuList.isShow)
					delayTimer = setTimeout(openMenu, delay, btn);
			}
		}
		
		/**
		 * 打开菜单
		 * @param p_target
		 * 
		 */		
		private function openMenu(p_target:TopBarButton):void
		{
			clearTimeout(delayTimer);
			var btn:TopBarButton = p_target;
			var cav:BorderContainer = btn.owner as BorderContainer;
			cav.setStyle("backgroundAlpha", 1);
			cav.styleName = "topMenuSelectedBG";
			if(!btn.menuList){
				btn.menuList = new MenuSublist();
				btn.menuList.relativeObj = btn;
				btn.menuList.dataProvider = btn.data;
			}
			btn.menuList.fixPositionCallback = fixPositionCallback;
			
			/*var cavmenu:BorderContainer = new  BorderContainer();*/
			var p:Point = cav.localToGlobal(new Point(btn.x, btn.y));
			btn.menuList.x = p.x - 10;
			btn.menuList.y = 60;
			/*btn.menuList.alpha = 0;*/
			
			FlexGlobals.topLevelApplication.addElement(btn.menuList);
			if(AppGlobal.effectOn)
				btn.menuList.isShow = true;
		}
		
		/**
		 * 修正位置后回调函数
		 * @param p_target
		 * 
		 */		
		private function fixPositionCallback(p_target:MenuSublist):void
		{
			if(AppGlobal.effectOn)
				playEffect(p_target, "show");
			else
				p_target.alpha = 1;
		}
		
		/**
		 * 播放渐进渐出效果
		 * @param p_target
		 * @param p_type
		 * 
		 */		
		private function playEffect(p_target:Object, p_type:String):void
		{
			var offset:Number = 30;
			var parallel:Parallel = new Parallel(p_target);
			parallel.duration = 100;
			parallel.addEventListener(EffectEvent.EFFECT_END, partalEndHandler);
			
			var move:Move = new Move();
			move.yFrom = p_type == "show" ? p_target.y + offset : p_target.y;
			move.yTo = p_type == "show" ? p_target.y : p_target.y + offset;
			
			var fade:Fade = new Fade();
			fade.alphaFrom = p_type == "show" ? 0 : 1;
			fade.alphaTo = p_type == "show" ? 1 : 0;
			fade.easingFunction = Linear.easeOut;
			
			parallel.children = [move, fade];
			parallel.play();
			
			function partalEndHandler(p_evt:EffectEvent):void
			{
				parallel.removeEventListener(EffectEvent.EFFECT_END, partalEndHandler);
				p_target.y = p_type == "show" ? p_target.y : p_target.y + offset;
				if(p_target.parent && p_type != "show")
					FlexGlobals.topLevelApplication.removeElement(p_target as IVisualElement);
			}
		}
		
		/**
		 * 延迟执行检测动作
		 * @param p_evt
		 * 
		 */		
		private function hideMenu(p_evt:MouseEvent):void
		{
			var btn:TopBarButton = p_evt.target as TopBarButton;		
			if(btn && !btn.isFocusIn){
				clearTimeout(btn.checkInterval);	
				if(btn.isFocusIn || !btn.menuList || btn.menuList.isFocusIn)return;
				btn.setStyle("color", 0xffffff);
				var cav:BorderContainer = btn.owner as BorderContainer;
				cav.setStyle("backgroundAlpha", 0);
				if(btn.menuList.parent){
					if(AppGlobal.effectOn){
						btn.menuList.isShow = false;
						playEffect(btn.menuList, "hide");
					}else
						FlexGlobals.topLevelApplication.removeElement(btn.menuList);
				}
			}
		}
		
		/**
		 * 鼠标滑出事件
		 * @param p_evt
		 * 
		 */	
		private function rollOutHandler(p_evt:MouseEvent):void
		{
			clearTimeout(delayTimer);
			var btn:TopBarButton = p_evt.target as TopBarButton;
			btn.isFocusIn = false;
			btn.checkInterval = setTimeout(hideMenu, 10, p_evt);
		}
		
		/**
		 * 移除所有子控件
		 * 
		 */		
		override public function removeAllElements():void
		{
			//移除所有事件监听器
			for(var i:uint = 0;i < numElements;i ++){
				var cav:BorderContainer = getElementAt(i) as BorderContainer;
				if(cav && cav.name != "seperator"){
					var btn:TopBarButton = cav.getElementAt(0) as TopBarButton;
					if(btn){
						btn.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
						btn.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
					}
				}
			}
			super.removeAllElements();
		}
	}
}


import spark.components.Label;

import components.custom.MenuSublist;

class TopBarButton extends Label
{
	public var menuList:MenuSublist;
	public var checkInterval:int;
	public var isFocusIn:Boolean;
	public var data:Object;
}